package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.domain.CheckGroup;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;
import cn.itcast.service.CheckGroupService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/checkGroup")
public class CheckGroupController {

    @Reference
    private CheckGroupService checkGroupService;


    /**
     * 添加检查组数据
     *
     * @param checkGroup
     * @param checkItemIds
     * @return
     */
    @RequestMapping("/add")
    public Result insertCheckGroup(@RequestBody CheckGroup checkGroup, Integer[] checkItemIds) {
        try {
            checkGroupService.insertCheckGroup(checkGroup, checkItemIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }
        return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }

    /**
     * 分页查询检查组信息
     *
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findByPage")
    public PageResult findByPage(@RequestBody QueryPageBean queryPageBean) {

        PageResult pageResult = checkGroupService.findByPage(queryPageBean);
        return pageResult;

    }

    /**
     * 删除
     *
     * @return
     */
    @RequestMapping("/deleteCheckGroup")
    public Result deleteCheckGroup(Integer id) {
        try {
            checkGroupService.deleteCheckGroup(id);
        } catch (Exception e) {
            return new Result(false, MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
        return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS);
    }

    /**
     * 查询当检查组,做数据回显
     *
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        CheckGroup checkGroup = checkGroupService.findByCheckGroupId(id);
        if (checkGroup == null) {
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        } else {
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkGroup);
        }
    }

    /**
     * 获取此检查组获取的所有检查项的ids,复选框的属性
     *
     * @param id
     * @return
     */
    @RequestMapping("/findByCheckItemOfCheckGroup")
    public Result findByCheckItemOfCheckGroup(Integer id) {
        List<Integer> ids = null;
        try {
            ids = checkGroupService.findByCheckItemOfCheckGroup(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, ids);
    }

    /**
     * 修改检查组
     *
     * @param checkGroup
     * @param checkItemIds
     * @return
     */
    @RequestMapping("/updateCheckGroup")
    public Result updateCheckGroup(@RequestBody CheckGroup checkGroup, Integer[] checkItemIds) {
        try {
            checkGroupService.updateCheckGroup(checkGroup, checkItemIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
    }

    /**
     * 查询所有的检查组信息
     *
     * @return
     */
    @RequestMapping("/findAllCheckGroup")
    public Result findAllCheckGroup() {
        List<CheckGroup> list = null;
        try {
            list = checkGroupService.findAllCheckGroup();
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, list);
    }


    /**
     * 批量删除
     *
     * @param params
     * @return
     */
    @RequestMapping("/deleteMoreCheckGroup")
    public Result deleteMoreCheckGroup(Integer[] params) {

        try {
            checkGroupService.deleteMoreCheckGroup(params);
        } catch (Exception e) {
            return new Result(false, MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
        return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS);
    }


}